--wifi连接配置页面

apRunConf={}
apRunConf.webFile="wifiAPConf.html"
initWifiApConfig={}


function WifiConnConfigWeb(req,res)
    local filename = ''
    if req.path == '/' then
        filename = apRunConf.webFile
    else
        filename = string.gsub(string.sub(req.path, 2), '/', '_')
    end
    res:sendFile(filename)
end

function WifiConnConfig(req,res)
    if req.query.ssid ~= nil and req.query.pwd ~= nil then
        print(req.query.ssid ..req.query.pwd)
        local wifiCfg ={}
        local mqttCfg ={}
        --获取Wifi参数
        wifiCfg.ssid=req.query.ssid
        wifiCfg.pwd=req.query.pwd
        --获取Mqtt参数
        mqttCfgStr=""
        mqttCfg.Ser=req.query.mqttSer
        mqttCfg.mqttName=req.query.mqttName
        mqttCfg.mqttPwd=req.query.mqttPwd
        mqttCfg.mqttPort=req.query.mqttPort
        mqttCfg.Topic=req.query.Topic
        if file.open(BootProcessVar.mqqtConfigFile,"w") then
            file.write(sjson.encode(mqttCfg))
            file:close()
            mqttCfgStr="Mqtt配置文件写入成功"
        else
            mqttCfgStr="Mqtt配置文件写入失败"
        end
        
        if file.open(BootProcessVar.UserWifiConnConfig,"w") then
            file.write(sjson.encode(wifiCfg))
            file:close()
            res:send('<head><meta charset="UTF-8"><title>配置终端</title></head><h1>配置完成、'..mqttCfgStr..'，正在重启</h1>')
                tmr.create():alarm(2000,tmr.ALARM_SINGLE,function() 
                    node.restart()
                end)
            return
        else
            res:send('<head><meta charset="UTF-8"><title>配置终端</title></head><h1>配置文件写入错误!!'..mqttCfgStr..'.</h1>')
            return
        end
    else
        res:send('<head><meta charset="UTF-8"><title>配置终端</title></head><h1>WIFI配置参数错误！！！</h1>')
    end
end

--AP配置，UserAP方式运行运行
function UserAPLevelRun()
    wifi.setmode(wifi.SOFTAP)
    local DeviceUserAP={}
    if file.open(BootProcessVar.UserAPConfig) then
        local finfo =file.read()
        --print("finfo"..finfo)
        DeviceUserAP= sjson.decode(finfo)
        file.close()
        print("Read User AP Config File End.")    
        local apCfg ={}
        apCfg.ssid=DeviceUserAP.ssid
        apCfg.pwd=DeviceUserAP.pwd
        apCfg.auth=DeviceUserAP.auth
        wifi.ap.config(apCfg)
        local apIpCfg={}
        apIpCfg.ip=DeviceUserAP.ip
        apIpCfg.netmask=DeviceUserAP.netmask
        wifi.ap.setip(apIpCfg)
        local apDnchCfg={}
        apDnchCfg.start=DeviceUserAP.start
        wifi.ap.dhcp.config(apDnchCfg)
        wifi.ap.dhcp.start()
        print("User AP Mode Run...")
    end
end

--以配置wifi模式运行
function InitWifiMode()
    UserAPLevelRun()
    collectgarbage("collect") --回收之前的内存
     tmr.create():alarm(5000,tmr.ALARM_SINGLE,function() 
        dofile(globalVar.webSer)  --加载web配置文件
        httpServer:use('/',WifiConnConfigWeb,"WifiConnConfigWeb")
        httpServer:use('/userWifiConfig',WifiConnConfig,"userWifiConfig")
        httpServer:listen(80)
        print("UserAP Start End...")
    end)
end

InitWifiMode()
